package com.study.ws.controller;

import com.study.ws.config.mq.RabbitMQConfiguration;
import com.study.ws.model.ps.entity.vo.PSMessageVO;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @author xl-9527
 * @since 2025/3/29
 **/
@Controller
public class SenderRabbitMQController {

    private final RabbitTemplate rabbitTemplate;

    public SenderRabbitMQController(final RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @PostMapping("rabbitmq/send")
    public ResponseEntity<?> sendMsg(@RequestBody PSMessageVO psMessage) {
        doSendMsg(psMessage);
        return ResponseEntity.ok("ok");
    }

    private void doSendMsg(final Object psMessage) {
        rabbitTemplate.convertAndSend(RabbitMQConfiguration.TOPIC_QUEUENAME, RabbitMQConfiguration.TOPIC_QUEUENAME, psMessage);
    }

    @MessageMapping("/mq-greeting")
    public String appHello(String message) {
        doSendMsg(message);
        return message;
    }
}
